<?php
/*
// +----------------------------------------------------------------------------------------
// | Icoterie Development Framework Intelligence Enterprise Management Priority Selective
// +----------------------------------------------------------------------------------------
// | Copyright (c) [2019] [Honor Full Epoch Educational Science Technology Hebei Co., Ltd.]
// | website  http://dev.icoterie.top http://www.ihfe.top
// +----------------------------------------------------------------------------------------
// | [Icoterie Development Framework] is licensed under the Mulan PSL v2.
// +----------------------------------------------------------------------------------------
// | You can use this software according to the terms and conditions of the Mulan PSL v2.
// +----------------------------------------------------------------------------------------
// | Licensed ( http://license.coscl.org.cn/MulanPSL2 )
// +----------------------------------------------------------------------------------------
// | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
// | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
// | FIT FOR A PARTICULAR PURPOSE.
// +----------------------------------------------------------------------------------------
// | See the Mulan PSL v2 for more details.
// +----------------------------------------------------------------------------------------
// | Author: Reflux Lewse,Red Shadow Sue
// +----------------------------------------------------------------------------------------
// | 视图引擎类
// +----------------------------------------------------------------------------------------
*/
namespace core\basic;
//use core\basic\Config;
class View
{
	private static $assign;
	private static $content;
	private static $view;
	public function __construct()
    {
		$this->assign = $assign;
		$this->content = $content;
		//echo '55 class'.PHP_EOL;
	}

	/**
     * 变量注入
     * @access	public
     * @param	string|array	$k	变量名
     * @param	mixed			$v	变量值
     * @return	$this
     */
    public function assign($k, $v = null)
    {
        if (is_array($k)) {
            $this->assign = array_merge($this->assign, $k);
        } else {
            $this->assign[$k] = $v;
        }
    }
	/**
     * 变量赋值
     * @access	public
     * @param	string	$k	变量名
     * @param	mixed	$v	变量值
     */
    public function setVar(string $k, $v)
    {
        $this->assign[$k] = $v;
    }

    /**
     * 变量提取
     * @access	protected
     * @param	string	$k	变量名
     * @return	mixed
     */
    public function getVar(string $k)
    {
		$view = new View();
		if (isset($view->assign[$k])) {
            return $view->assign[$k];
        }
		var_dump($view->assign);
    }
	/** 
	* 模板解析
	* @param	string	$f	模板文件
	*/
    public function dispaly($f)  
    {
		$t = self::cache($f);/*读取缓存*/
		self::fetch('cache',$t,$this->assign);/*渲染页面*/
		$c = ob_get_contents();// ob_end_clean();
		self::state(md5($f),$c);/*生成静态*/
    }
	/**
     * 缓存模板文件
     * @access	public
     * @param	string	$f	模板文件
     * @echo	string  $t	缓存文件
     */
	public static function cache($f){
		ob_start();
		$c = self::fetch('temp',$f);/*读模板*/
		$c = self::compile($c);/*编译模板*/
		$t = TEMP_PATH . md5($f) . '.php';
		if(file_exists($t) || filemtime($t) < filemtime($f)){
			file_put_contents($t, $c) ?: ex('编译文件' . $t . '生成出错！请检查目录是否有可写权限！');/*写缓存*/
		}
		ob_clean();
		return $t;
    }
	/**
     * 渲染模板文件
     * @access	public
     * @param	string	$f	模板文件
     * @param	array	$d	模板变量
     */
    public static function fetch(string $a = 'render', string $f, array $d = [])
    {
        if (!is_file($f)) ex('template not exists:' . $f);/*模板不存在 抛出异常*/
		switch ($a) {
            case 'cache':
				extract($d, EXTR_OVERWRITE);
				include_once $f;
                break;
            case 'temp':
				return $d = file_get_contents($f);
                break;
            default:
                //$p = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';
        }
    }
	
	
	public static function compile($c){
		self::$content = $c;
		$r = Config::get('compile');
		foreach($r as $k=>$v){
			if (preg_match($k, self::$content)) {
				self::$content = preg_replace($k, $v, self::$content);
			}
		}
		return self::$content;
    }
	
	public static function state($f,$c){
		$t = TEMP_PATH . $f . '.php';
		$s = CACHE_PATH . $f . '.php';
		if(!file_exists($s) || filemtime($s) < filemtime($t)){
			$c = self::gzip($c);
			file_put_contents($s, $c) ?: ex('编译文件' . $s . '生成出错！请检查目录是否有可写权限！');/*写静态*/
		}
    }
	
	public static function gzip($c)
    {
		$c = ltrim(
			rtrim(
				preg_replace(
					array(
						"/> *([^ ]*) *</",
						"//",
						"'/\*[^*]*\*/'",
						"/\r\n/",
						"/\n/",
						"/\t/",
						'/>[ ]+</'
					),
					array(
						">\\1<",
						'',
						'',
						'',
						'',
						'',
						'><'
					),
				$c)
			)
		);
		
        return $c;
    }
}
?>